package com.gjintel.business.webadv.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.business.webadv.model.WebAdv;
import com.gjintel.business.webadv.service.IWebAdvService;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

@RequestMapping("/business/webadv")
@Controller
public class WebAdvController {

	private static final Logger LOG = Logger.getLogger(WebAdvController.class);
	@Autowired
	private IWebAdvService webAdvService;

	/**
	 * @description 获得输入数据信息
	 * @author fengya
	 * @date 2016-3-15 下午03:55:05
	 * @param id
	 * @return
	 * @return ModelAndView`
	 */
	@RequestMapping("/input")
	public ModelAndView input(String id) {
		ModelAndView mv = new ModelAndView("/business/webadv/adv-type-input");
		if (!StringUtil.isEmpty(id)) {
			WebAdv adv = webAdvService.get(WebAdv.class, id);
			mv.addObject("adv", adv);
		}
		return mv;
	}

	/**
	 * @description 得的所有的记录数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:22
	 * @return
	 * @return JSONArray
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public JSONArray listAll() {
		List<WebAdv> list = this.webAdvService.findAll();
		return JSONArray.fromObject(list);
	}

	/**
	 * @description 分页查询数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:40
	 * @param page
	 * @param goodsType
	 * @param sort
	 * @return
	 * @return JSONObject
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, WebAdv adv, Sort sort) {
		this.webAdvService.findAll(page, adv, sort);
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @description 保存数据
	 * @author Ant-K
	 * @date 2016-3-15 下午03:55:52
	 * @param session
	 * @param goodsType
	 * @return
	 * @return String
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, WebAdv adv) {
		User user = (User) session.getAttribute("loginUser");
		if (adv != null) {
			if (StringUtil.isEmpty(adv.getId())) {
				adv.setId(null);
				adv.setCreateTime(new Date());
				adv.setCreaterName(user.getName());
				adv.setCreaterId(user.getId());
			}
			adv.setUpdateTime(new Date());
			adv.setUpdaterName(user.getName());
			adv.setUpdaterId(user.getId());
			this.webAdvService.saveOrUpdate(adv);
			LogUtil.addLog(new LogInfo("添加广告导航数据", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()), adv.getClass().getSimpleName(), adv.getId()));

			return Constant.SAVE_SUCCESS;
		}
		return Constant.SAVE_UN_SUCCESS;
	}

	/**
	 * @description 删除数据
	 * @author fengya
	 * @date 2016-3-15 下午03:56:08
	 * @param deleteIds
	 * @return
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(HttpSession session, String deleteIds) throws Exception {
		this.webAdvService.removeData(deleteIds);
		User user = (User) session.getAttribute("loginUser");
		LogUtil.addLog(new LogInfo("删除广告数据", user.getId(), user.getName(), String.valueOf(LogType.DELETE.ordinal()), WebAdv.class.getSimpleName(), deleteIds));
		return Constant.DEL_SUCCESS;
	}

}
